/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.bailian.service.impl;

import me.zhengjie.modules.bailian.domain.BailianApplication;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.bailian.service.BailianApplicationService;
import me.zhengjie.modules.bailian.domain.dto.BailianApplicationQueryCriteria;
import me.zhengjie.modules.bailian.mapper.BailianApplicationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author Yaco
* @date 2025-03-15
**/
@Service
@RequiredArgsConstructor
public class BailianApplicationServiceImpl extends ServiceImpl<BailianApplicationMapper, BailianApplication> implements BailianApplicationService {

    private final BailianApplicationMapper bailianApplicationMapper;

    @Override
    public PageResult<BailianApplication> queryAll(BailianApplicationQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(bailianApplicationMapper.findAll(criteria, page));
    }

    @Override
    public List<BailianApplication> queryAll(BailianApplicationQueryCriteria criteria){
        return bailianApplicationMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BailianApplication resources) {
        bailianApplicationMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BailianApplication resources) {
        BailianApplication bailianApplication = getById(resources.getId());
        bailianApplication.copy(resources);
        bailianApplicationMapper.updateById(bailianApplication);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        bailianApplicationMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<BailianApplication> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BailianApplication bailianApplication : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("业务空间ID", bailianApplication.getBusinessSpaceId());
            map.put("应用ID", bailianApplication.getAppId());
            map.put("应用名称", bailianApplication.getAppName());
            map.put("API-KEY", bailianApplication.getApiKey());
            map.put("RAM accesskey ID", bailianApplication.getRamAccesskeyId());
            map.put("RAM accesskey secret", bailianApplication.getRamAccesskeySecret());
            map.put("记录创建时间", bailianApplication.getCreatedAt());
            map.put("记录修改时间", bailianApplication.getUpdatedAt());
            map.put("记录状态 (0: 不可用, 1: 可用)", bailianApplication.getRecordStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
